
<p:config xmlns:p="http://www.orbeon.com/oxf/pipeline" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:oxf="http://www.orbeon.com/oxf/processors" xmlns:xforms="http://www.w3.org/2002/xforms" xmlns:xxforms="http://orbeon.org/oxf/xml/xforms" xmlns:xdb="http://orbeon.org/oxf/xml/xmldb" xmlns:xu="http://www.xmldb.org/xupdate">

    <p:param type="output" name="data"/>

    <p:processor name="oxf:request">
        <p:input name="config">
            <config >
                <include>/request/parameters</include>
                <include>/request/request-uri</include>
            </config>
        </p:input>
        <p:output name="data" id="request"/>
    </p:processor>

    <p:choose href="#request">
        <p:when test="/request/parameters/parameter[name = 'widget']/value/text() != ''">
            <p:processor name="oxf:xslt">
                <p:input name="data" href="#request"/>
                <p:output name="data" id="file-addr"/>
                <p:input name="config">
                    <config xsl:version="2.0">
                        <url>
                            <xsl:value-of select="/request/parameters/parameter[name = 'widget']/value/text()"/>
                        </url>
                        <xsl:copy-of select="/request/parameters/parameter[name='widget']/content-type"/>
                    </config>
                </p:input>
            </p:processor>

            <p:processor name="oxf:url-generator">
                <p:input name="config" href="#file-addr"/>
                <p:output name="data" id="file"/>
            </p:processor>

            <p:processor name="oxf:unzip">
                <p:input name="data" href="#file"/>
                <p:output name="data" id="zip-file-list"/>
            </p:processor>

			<!--Read config file-->
            <p:processor name="oxf:xslt">
                <p:input name="data" href="#zip-file-list"/>
                <p:output name="data" id="config_url"/>
                <p:input name="config">
                    <config xsl:version="2.0">
                        <url>
                            <xsl:value-of select="/files/file[@name='config.xml']"/>
                        </url>
                        <content-type>text/xml</content-type>
                    </config>
                </p:input>
            </p:processor>
            <p:processor name="oxf:url-generator">
                <p:input name="config" href="#config_url"/>
                <p:output name="data" id="validate"/>
            </p:processor>

			<!--manifest validation-->
            <p:processor name="oxf:validation">
                <p:input name="config">
                    <config>
                        <decorate>true</decorate>
                    </config>
                </p:input>
                <p:input name="schema" href="/schema/manifest.xsd"/>
                <p:input name="data" href="#validate"/>
                <p:output name="data" id="manifest"/>
            </p:processor>

            <p:choose href="#manifest">
				<!--Manifest error-->
                <p:when test="exists(//*:error)">
                    <p:processor name="oxf:xslt">
                        <p:input name="data" href="#manifest"/>
                        <p:output name="data" id="error_doc"/>
                        <p:input name="config" href="/views/manifest_debug.xsl"/>
                    </p:processor>
                    <p:processor name="oxf:xml-converter">
                        <p:input name="config">
                            <config>
                                <content-type>text/html</content-type>
                                <encoding>utf-8</encoding>
                                <version>1.0</version>
                                <omit-xml-declaration>true</omit-xml-declaration>
                            </config>
                        </p:input>
                        <p:input name="data" href="#error_doc"/>
                        <p:output name="data" id="html-doc"/>
                    </p:processor>

                    <p:processor name="oxf:http-serializer">
                        <p:input name="config">
                            <config>
                                <content-type>text/html</content-type>
                                <force-content-type>true</force-content-type>
                            </config>
                        </p:input>
                        <p:input name="data" href="#html-doc"/>
                    </p:processor>

                    <p:processor name="oxf:identity">
                        <p:input name="data">
                            <error>An error occured during widget installation.</error>
                        </p:input>
                        <p:output name="data" id="message"/>
                    </p:processor>
                </p:when>
                <p:otherwise>
                    
                    <!--Get absolute path to resources directory-->
                    <p:processor name="oxf:directory-scanner">
                        <p:input name="config">
                            <config>
                                <base-directory>../../..</base-directory>
                                <exclude>**/*</exclude>
                                <case-sensitive>false</case-sensitive>
                            </config>
                        </p:input>
                        <p:output name="data" id="resources"/>
                    </p:processor>

                    <p:processor name="oxf:xslt">
                        <p:input name="data" href="#zip-file-list"/>
                        <p:input name="manifest" href="#manifest"/>
                        <p:input name="resources" href="#resources"/>
                        <p:output name="data" id="files-list"/>
                        <p:input name="config">
                            <xsl:stylesheet version="2.0">
                                <xsl:template match="/">
                                    <xsl:copy>
                                        <xsl:apply-templates select="@*|node()"/>
                                    </xsl:copy>
                                </xsl:template>
                                <xsl:template match="file[ends-with(@name, '/')]"/>
                                <xsl:template match="file" priority="-2">
                                    <file name="{concat(doc('input:resources')/directory/@path,'/widgets/',string(doc('input:manifest')//@id[1]), '/', @name)}">
                                        <xsl:apply-templates select="@*|node()"/>
                                    </file>
                                </xsl:template>
                                <xsl:template match="@name"/>
                                <xsl:template match="@*|node()" priority="-3">
                                    <xsl:copy>
                                        <xsl:apply-templates select="@*|node()"/>
                                    </xsl:copy>
                                </xsl:template>
                            </xsl:stylesheet>
                        </p:input>
                    </p:processor>

                    <p:processor name="oxf:xslt">
                        <p:input name="data" href="#manifest"/>
                        <p:input name="cat" href="#request#xpointer(/request/parameters/parameter[name='category'])"/>
                        <p:output name="data" id="query"/>
                        <p:input name="config">
                            <xdb:query xsl:version="2.0" collection="/db/portal">
                                <xsl:variable name="cat" select="doc('input:cat')/parameter/value"/>
                                <xsl:text>xquery version "1.0";</xsl:text>
				let $id := '<xsl:value-of select="/*:widget/@id"/>' return
				if ( exists(/widgetsList/category/widget[@id=$id])) then <error>An error occured during widget installation.</error> else <message>{update insert <widget id='{/*:widget/@id}'>
                                        <name>
                                            <xsl:value-of select="/*:widget/*:title"/>
                                        </name>
                                        <visible>1</visible>
                                        <authkey/>
									</widget>into /widgetsList/category[@id='<xsl:value-of select="$cat"/>']}Widget '<xsl:value-of select="/*:widget/*:title"/>' has been successfully updated.</message>
                            </xdb:query>
                        </p:input>
                    </p:processor>

					<!--create config to write-->
                    <p:for-each select="/files/file" href="#files-list" root="configs" id="configs">

                        <p:processor name="oxf:xslt">
                            <p:input name="data" href="current()"/>
                            <p:output name="data" id="url"/>
                            <p:input name="config">
                                <xsl:stylesheet version="2.0">
                                    <xsl:template match="/">
                                        <config>
                                            <url>
                                                <xsl:value-of select="."/>
                                            </url>
                                            <content-type>application/octet-stream</content-type>
                                        </config>
                                    </xsl:template>
                                </xsl:stylesheet>
                            </p:input>
                        </p:processor>
                        <p:processor name="oxf:url-generator">
                            <p:input name="config" href="#url"/>
                            <p:output name="data" id="tmp_file"/>
                        </p:processor>

                        <p:processor name="oxf:xslt">
                            <p:input name="data" href="current()"/>
                            <p:output name="data" id="config"/>
                            <p:input name="config">
                                <xsl:stylesheet version="2.0">
                                    <xsl:variable name="path" select="tokenize(file/@name, '/')"/>
                                    <xsl:template match="/">
                                        <config>
                                            <file>
                                                <xsl:value-of select="file/@name"/>
                                            </file>
                                            <make-directories>true</make-directories>
                                            <append>false</append>
                                            <content-type>application/octet-stream</content-type>
                                        </config>
                                    </xsl:template>
                                </xsl:stylesheet>
                            </p:input>
                        </p:processor>

                        <p:processor name="oxf:file-serializer">
                            <p:input name="config" href="#config"/>
                            <p:input name="data" href="#tmp_file"/>
                        </p:processor>

                        <p:processor name="oxf:identity">
                            <p:input name="data" href="#config"/>
                            <p:output name="data" ref="configs"/>
                        </p:processor>
                    </p:for-each>

                    <p:processor name="oxf:null-serializer">
                        <p:input name="data" href="#configs"/>
                    </p:processor>


                    <p:processor name="oxf:xmldb-query">
                        <p:input name="datasource" href="/config/datasource.xml"/>
                        <p:input name="query" href="#query"/>
                        <p:output name="data" id="message"/>
                    </p:processor>
                </p:otherwise>
            </p:choose>
        </p:when>
        <p:otherwise>
            <p:processor name="oxf:identity">
                <p:input name="data">
                    <error>An error occured during widget installation.</error>
                </p:input>
                <p:output name="data" id="message"/>
            </p:processor>
        </p:otherwise>
    </p:choose>

    <p:processor name="oxf:pipeline">
        <p:input name="config" href="../widget.xpl"/>
        <p:output name="data" id="datas"/>
    </p:processor>

    <p:processor name="oxf:identity">
        <p:input name="data" href="aggregate('data', #message, #datas#xpointer(/data/*))"/>
        <p:output name="data" ref="data"/>
    </p:processor>
</p:config>